package com.freak.wanandroid.base.dialog;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.utils.TUtil;


/**
 * MVVM模式的Base Dialog fragment
 *
 * @author freak
 * @date 2022/09/12.
 */
public abstract class BaseLifecycleDialogFragment<T extends BaseViewModel> extends BaseDialogFragment {

    protected T mViewModel;

    /**
     * 数据绑定
     */
    protected abstract void dataObserver();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mViewModel = viewModelProviders(this, (Class<T>) TUtil.getInstance(this, 0));
        if (null != mViewModel) {
            dataObserver();
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * create ViewModelProviders
     *
     * @return ViewModel
     */
    protected <T extends ViewModel> T viewModelProviders(BaseLifecycleDialogFragment fragment, @NonNull Class<T> modelClass) {
        return new ViewModelProvider(fragment).get(modelClass);
    }
}
